package com.moriarty.socketim.activity;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.moriarty.socketim.R;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private Button mBtnLink;
    private Socket mSocket;
    private Button mBtnSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnLink = (Button) findViewById(R.id.btn_link);
        mBtnSend = (Button) findViewById(R.id.btn_send);

        mBtnLink.setOnClickListener(this);
        mBtnSend.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_link:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //连接服务器
                        try {
                            //这一句就够了 默认就会连接上来
                            mSocket = new Socket("192.168.0.102", 5225);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case R.id.btn_send:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            DataOutputStream dataOutputStream = new DataOutputStream(mSocket.getOutputStream());
                            dataOutputStream.writeUTF("哥来了");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
        }
    }
}
